package buaa.leetcode.List;

import buaa.leetcode.utilBeans.ListNode;

public class MergeTwoSortedLists {
	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null){
        	return l1 == null ? l2 : l1;
        }
        ListNode head = (l1.val<l2.val)?l1:l2;
        ListNode tmpNode = (l1.val<l2.val)?l2:l1;
        ListNode prevNode = head;
        ListNode currNode = head.next;
        while(currNode != null && tmpNode != null){
        	if(currNode.val <= tmpNode.val){
        		prevNode = currNode;
        		currNode = currNode.next;
        	}else{
        		prevNode.next = tmpNode;
        		prevNode = tmpNode;
        		tmpNode = currNode;
        		currNode = prevNode.next;
        	}
        }
        if(tmpNode == null){
        	return head;
        }else{
        	prevNode.next = tmpNode;
        	return head;
        }
    }
}
